public class BlockingQueue {
    public int[] elem=new int[1000];
    public volatile int size;
    public int head;
    public int tail;
    public void put(int value) throws InterruptedException {
        synchronized (this){
            while(size==elem.length){
                wait();
            }
            tail=(tail+1)/elem.length;
            elem[tail]=value;
            size++;
            notifyAll();
        }
    }
    public int take() throws InterruptedException {
        int value=0;
        synchronized (this){
            while(size==0){
                wait();
            }
            value=elem[head];
            head=(head+1)/elem.length;
            notifyAll();
        }
        return value;
    }

    public static void main(String[] args) {
        BlockingQueue blockingQueue=new BlockingQueue();
        Thread customer=new Thread(()->{
            while(true) {
                int value = 0;
                try {
                    value = blockingQueue.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(value);
            }
        });
        customer.start();
        Thread producer=new Thread(()->{
            while(true){
                int value=0;
                try {
                    blockingQueue.put(value);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("生产"+value);
                value++;

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        producer.start();


    }

}
